﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.Util;
using Com.CMS263.PO;
using Com.CMS263.BLL;
namespace Com.CMS263.Web.manage.adminUser
{
    public partial class AddUserRole : System.Web.UI.Page
    {
        public AdminRoleBLL arbll = new AdminRoleBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }
        public void BindData()
        {
            IList<AdminRole> arlist = new List<AdminRole>();
            arlist = arbll.GetAllAdminRole();
            GridView1.DataSource = arlist;
            GridView1.DataBind();
        }
        public AdminRoleBLL urb = new AdminRoleBLL();

        public int ArId
        {
            get { return Convert.ToInt32(ViewState["aid"]); }
            set { ViewState["aid"] = value; }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {


            if (this.roleName.Text == "")
            {
                StringUtil.Alert("请填写角色名", "-1");
            }
            else if (this.roleName.Text.Length > 50)
            {
                StringUtil.Alert("角色名不能多于50个字符", "-1");
            }
            string roleName = StringUtil.checkStr(this.roleName.Text);

            string power = Request["power"];
            if (power == null)
            {
                power = "";
            }
            AdminRole userRole = new AdminRole();

            userRole.roleName = roleName;
            userRole.rolePower = power;

            if (!urb.isExist(roleName))
            {
                try
                {
                    urb.Add(userRole);

                    StringUtil.Alert("添加成功", "AddUserRole.aspx");
                }
                catch
                {
                    StringUtil.Alert("添加失败", "-1");
                }
            }
            else
            {
                StringUtil.Alert("此角色名已经存在", "-1");
            }


        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (id > 0)
            {
                string act = e.CommandName.ToString();
                if (act.Equals("btnPower"))
                {
                    Response.Redirect("/manage/control.aspx?rid=" + id);
                }
                else if (act.Equals("btnDel"))
                {
                    try
                    {AdminRole ab = arbll.GetAdminUserByID(id);
                        arbll.DeleteUserById(id);
                        

                        AdminLoginInfo ali = AdminLoginCookie.GetLoginInfo();
                        ArtTypeBLL atbll = new ArtTypeBLL();
                        OperationLogBLL olbll = new OperationLogBLL();
                        OperationLog ol = new OperationLog();
                        ol.AddDate = DateTime.Now;
                        ol.Ip = StringUtil.GetIP();
                        ol.ModuleName = "删除管理员角色";
                        ol.Contents = "【删除】" + ab.roleName;
                        ol.UserName = ali.UserName;
                        olbll.Add(ol);

                        StringUtil.Alert("删除成功", "AddUserRole.aspx");

                    }
                    catch 
                    {
                        StringUtil.Alert("删除失败", "-1");
                    }
                }
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lb1 = e.Row.FindControl("Label1") as Label;
                if (lb1.Text == "超级管理员")
                {
                    lb1.Font.Bold = true;
                    Button lb = e.Row.FindControl("btnDel") as Button;
                    lb.Enabled = false;
                }
            }
        }
    }
}
